package com.nf.health.app.activity.doctor_advice.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.activity.doctor_advice.adapter.DoctorAdviceAdapter;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.customview.DragListView.onRefreshAndLoadMoreListener;
import com.nf.health.app.models.BannerList;
import com.nf.health.app.models.DoctorAdviceItem;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.zxing.CaptureActivity;

import java.util.ArrayList;
import java.util.List;


/**
 * @author Ian
 * @version V1.3
 * @Description: MyDocAdviceActivity
 * @date 2015-10-21 下午2:40:22
 */
public class MyDocAdviceActivity extends BaseActivity implements OnPageChangeListener, OnClickListener {

    private TextView tv_title;
    private DragListView listview;
    private int pageNo = 1;
    private int pageSize = 10;
    private int PAGE_SIZE = 0;
    private List<DoctorAdviceItem> datas = new ArrayList<DoctorAdviceItem>();
    private List<RadioButton> rbIndicator = new ArrayList<RadioButton>();
    List<DoctorAdviceItem> myDoctor = new ArrayList<DoctorAdviceItem>();
    private DoctorAdviceAdapter adapter;
    private List<BannerList> banners;
    private ImageView right_view;
    private TextView emptyView;

    int cusPos;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTitleBar(R.layout.titlebar_base);//  title 栏
        setContent(R.layout.activity_my_doc_advice);
        initView();
        initListener();
    }

    private void initView() {
        tv_title = (TextView) findViewById(R.id.titlebar_title_tv);
        tv_title.setText("我的医嘱");

        right_view = (ImageView) findViewById(R.id.titlebar_right_view);
        listview = (DragListView) findViewById(R.id.listview);


        //visible ImageView
        right_view.setImageResource(R.drawable.home_scan);
        right_view.setVisibility(View.VISIBLE);

        emptyView = new TextView(this);
        emptyView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        emptyView.setText("您没有医嘱！");
        emptyView.setGravity(Gravity.CENTER);
        emptyView.setTextColor(getResources().getColor(R.color.black9));
        emptyView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        emptyView.setVisibility(View.GONE);
        ((ViewGroup) listview.getParent()).addView(emptyView);
    }


    public void initData(String tag) {
        provider.requestDOCTOR_ADVICE_LIST(pageNo + "", pageSize + "", "", tag);//判断是否是医生聊天窗口  有doctorid  进行传值
    }

    private void initListener() {


        right_view.setOnClickListener(this);
        listview.setRefreshableAndLoadMoreable(true, false);
        listview.setOnRefreshAndLoadMoreListener(new onRefreshAndLoadMoreListener() {

            @Override
            public void onRefresh() {
                pageNo = 1;
                initData("refresh");
            }

            @Override
            public void onLoadMore() {
                pageNo++;
                initData("more");
            }
        });
//		listview.setOnItemClickListener(new OnItemClickListener() {
//
//			@Override
//			public void onItemClick(AdapterView<?> parent, View view,
//					int position, long id) {
//				//listview存在刷新的头和viewpager的头，所以减2
//				Bundle bundle = new Bundle();
//				String autoid = datas.get(position-1).getAutoid();
//				bundle.putString(WebViewActivity.KEY_URL, Apis.HOST_COMMON+"/"+autoid+"/index.html");
//				bundle.putString(WebViewActivity.KEY_TITLE,"检测报告");
//				ActivityUtils.showActivity(MyDocAdviceActivity.this, WebViewActivity.class, bundle);
//			}});
    }

    @SuppressWarnings("unchecked")
    @Override
    public void handleActionSuccess(String actionName, Object object) {

        super.handleActionSuccess(actionName, object);
        if ("refresh".equals(actionName)) {
            listview.completeLoadMore();
            myDoctor.clear();
            myDoctor = (List<DoctorAdviceItem>) object;
            datas.clear();
            if (myDoctor != null) {
                datas.addAll(myDoctor);
            }
            if (datas == null || datas.size() <= 0) {
                listview.setEmptyView(emptyView);
            } else {
                if (myDoctor.size() < pageSize) {
                    listview.setRefreshableAndLoadMoreable(true, false);
                } else {
                    listview.setRefreshableAndLoadMoreable(true, true);
                }
            }
            adapter = new DoctorAdviceAdapter(MyDocAdviceActivity.this, datas);
            listview.setAdapter(adapter);
        } else if ("more".equals(actionName)) {
            listview.completeLoadMore();
            myDoctor.clear();
            myDoctor = (List<DoctorAdviceItem>) object;

            if (myDoctor.size()>0) {
                datas.addAll(myDoctor);
                adapter.notifyDataSetChanged();
            } else {
                showToast("没有更多数据~");
            }
            if (myDoctor.size() < pageSize) {
                listview.setRefreshableAndLoadMoreable(true, false);
            } else {
                listview.setRefreshableAndLoadMoreable(true, true);
            }

            //删除
        } else if (actionName.equals("GETDATABANNERLIST")) {

        } else if ("Delete_DocAdvice".equals(actionName)) {
            showToast("删除成功");
            datas.remove(cusPos);
            adapter.notifyDataSetChanged();//更新数据

            if (datas.size() <= 0) {
                initData("refresh");
            }
        }
        adapter.setOnMyClickListener(new OnMyClickListener() {//接口回调删除

            @Override
            public void onClick(int position, String flag) {
                cusPos = position;
                provider.requestDeleteDocAdvice(datas.get(position).getId(), "Delete_DocAdvice");
            }
        });
    }


    @Override
    public void handleActionFinish(String actionName, Object object) {
        listview.completeLoadMore();
        listview.completeRefresh();
        hideLoading();
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    @Override
    public void onPageSelected(int position) {
        rbIndicator.get(position).setChecked(true);
        //		vp_title.setText(banners.get(position).getB_name());
    }

    @Override
    public void onResume() {
        initData("refresh");
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.titlebar_right_view:
                ActivityUtils.showActivityForResult(MyDocAdviceActivity.this, CaptureActivity.class, 100);
                break;

            case R.id.titlebar_back_view:
                finish();
                break;

            default:
                break;
        }
        super.onClick(v);
    }

}
